package com.ruoyi.luban.controller;

import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.luban.domain.PDealer;
import com.ruoyi.luban.domain.dto.LoginDto;
import com.ruoyi.luban.domain.vo.LoginUserAndTokenVo;
import com.ruoyi.luban.service.IPDealerService;
import com.ruoyi.luban.service.IPWxLoginUserService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/userLogin")
public class LoginController extends WXBaseController {
    @Autowired
    private IPWxLoginUserService ipWxLoginUserService;
    @Autowired
    private IPDealerService ipDealerService;
    /**
     * 经销商绑定登录
     * @param dto
     * @return
     */
    @PostMapping("/bindLoginName")
    @Log(title = "经销商账号登录", businessType = BusinessType.INSERT)
    public AjaxResult bindLoginName(@RequestHeader(value = "token",defaultValue = "test")  String token,@RequestBody LoginDto dto){
        try {
            logger.info("经销商账号登录:{}", JSONObject.toJSONString(dto));
            LoginUserAndTokenVo info = this.getLoginUserInfo2(token);
            LoginUserAndTokenVo loginUserAndTokenVo = ipWxLoginUserService.bindLoginName(info,dto);
            if (loginUserAndTokenVo != null) {
                if(loginUserAndTokenVo.getLoginType().equals("-2")){
                    return error("用户账号已绑定，请勿重新其他账号");
                }else   if(loginUserAndTokenVo.getLoginType().equals("-3")){
                    return error("用户密码错误，请重新输入");
                }else  if(loginUserAndTokenVo.getLoginType().equals("-4")){
                    return error("该经销商账号已被绑定，请绑定其他账号");
                }else  if(loginUserAndTokenVo.getLoginType().equals("-5")){
                    return error("该经销商账号已被注销，请绑定其他账号");
                }
                setLoginUserInfo2(loginUserAndTokenVo);
                return success(loginUserAndTokenVo);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("账号登录失败");
        }
        return error("用户账号不存在，或密码错误，请重新输入");
    }

    /**
     * 获取经销商信息
     * @param token
     * @return
     */
    @GetMapping("/getDealer")
    @Log(title = "经销商账号登录", businessType = BusinessType.INSERT)
    public AjaxResult getDealer(@RequestHeader(value = "token",defaultValue = "test")  String token){
        try {
            LoginUserAndTokenVo info = this.getLoginUserInfo2(token);
            if(info == null){
                return error_to_login();
            }
            return ipDealerService.getDealer(info);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("我的资料获取失败");
        }
        return error("获取登录绑定经销商账户失败");
    }
    /**
     * 退出登录
     * @param token
     * @return
     */
    @PostMapping("/logout")
    @Log(title = "注销登录", businessType = BusinessType.INSERT)
    public AjaxResult logout(@RequestHeader(value = "token",defaultValue = "test")  String token){
        ipWxLoginUserService.logout(token);
        return success();
    }
}
